package ClassRoom;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

public class Demo1 {
    public static void main(String[] args) throws Exception {
        SAXReader reader = new SAXReader();
        Document document= reader.read(new FileInputStream("Day18\\xml\\city.xml"));
        System.out.println(document);
        //获取根标签
        Element rootElement = document.getRootElement();
        //获取根标签属性
        Attribute Name = rootElement.attribute("Name");
        System.out.println(Name.getValue());
        //获取根标签下面的子标签，返回一个集合
        List<Element> elements = rootElement.elements();
        //获取第5个city长沙的值
        Element element = elements.get(5);
        System.out.println(element.getStringValue());
        //获取第二个city里面的内容
        Element element1 = elements.get(1);
        String text = element1.getText();
        System.out.println(text);
        //获取第一个city里面的属性值
        Element element2 = elements.get(0);
        Attribute id = element2.attribute("id");
        System.out.println(id.getValue());
    }
}
